package com.sghj.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class timeUtils {
    //获取当前时间所处季度
    public  int getCurrentQuarter(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始，需要加1
        return (month - 1) / 3 + 1;
    }
    //获取当前日期
    //取出日期的方法
    public static String getDate(Date longDate) throws ParseException {
        String[] strNow3 = new SimpleDateFormat("yyyy-MM-dd").format(longDate).toString().split("-");
        String stime=strNow3[0]+"-"+strNow3[1]+"-"+strNow3[2];
        return stime;
    }


    //取出日期的方法
    public String getDateHZ(Date longDate) throws ParseException {
        String[] strNow3 = new SimpleDateFormat("yyyy-MM-dd").format(longDate).toString().split("-");
        String stime=strNow3[0]+"年"+strNow3[1]+"月"+strNow3[2]+"日";
        return stime;
    }
    //String转date
    public Date stringtoDate(String time) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Date date = simpleDateFormat.parse(time);
        return date;

    }

    //String转date带时分秒
    public Date stringtoDateTime(String time) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date = simpleDateFormat.parse(time);
        return date;

    }
    //取出年
    public String getDateYear(Date longDate) throws ParseException {
        String[] strNow3 = new SimpleDateFormat("yyyy-MM-dd").format(longDate).toString().split("-");
        String stime=strNow3[0];
        return stime;
    }
    //当前日期是否在季度月的15号
    public Map<String,String> isAfter15(Date nowDate) throws ParseException {
//        System.out.println("现在"+nowDate);
        String nowD=getDate(nowDate);
//        System.out.println("现在日期"+nowD);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Date date = simpleDateFormat.parse(nowD);

        Map<String,String> map=new HashMap<>();

        //当前年份
        String year=getDateYear(nowDate);
        boolean b;
        Date base=new Date();

        //判断在第几个季度
        int season=getCurrentQuarter(nowDate);
        map.put("season", String.valueOf(season));

        switch (season) {
            case 1:
                //一季度:3月15日
                String baseline=year+"-"+"03-15";
                base=stringtoDate(baseline);
                break;
            case 2:
                //季度:3月15日
                String baseline2=year+"-"+"06-15";
                base=stringtoDate(baseline2);
                //晚于这一天，则返回true

                break;
            case 3:
                String baseline3=year+"-"+"09-15";
                base=stringtoDate(baseline3);
                // code to be executed if expression = value2;
                break;
            case 4:
                String baseline4=year+"-"+"12-15";
                base=stringtoDate(baseline4);
                // code to be executed if expression = value2;
                break;
    }
    if(date.after(base)){
        map.put("result","true");
    }else {
        map.put("result","false");
    }
    return map;
    }

    //判断是否可查
    public boolean canbeDownload(Integer year,Integer season) throws ParseException {
        //当前年份
        Date now=new Date();
        Integer curYear= Integer.valueOf(getDateYear(now));
        //当前季度
        Integer curSeason=getCurrentQuarter(now);
        //判断年份
        if(year<curYear){
            return true;
        }else if(year>curYear){
            return false;
        }else {
            //相等的情况，判断季度
            if(season<curSeason){
                return true;
            }else if(season>curSeason){
                return false;
            }else {
                //季度相等时，判断是否在季度月的15号之后
                Map<String,String> isAfter15R=isAfter15(now);
                if(isAfter15R.get("result").equals("true")){
                    return true;
                }else {
                    return false;
                }
            }
        }
    }

}
